<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>回话2</title>
    <script src="../assets/NIM_Web_SDK_v5.2.0.js"></script>


    <script>
        var account = "002"
        var to_account = "001"

        var token = "9cffa850d665e64383bc8517a8f718a3"


        var appkey = "9025d22f37f68da19249897b75922a11";

        var data = {};
        function initSDK() {
            window.nim = SDK.NIM.getInstance({
                appKey: appkey,
                account: account,
                token: token,
                onfriends: onFriends,
                onofflinesysmsgs: onOfflineSysMsgs,
                onsysmsg: onSysMsg,
                onupdatesysmsg: onUpdateSysMsg,
                onsysmsgunread: onSysMsgUnread,
                onupdatesysmsgunread: onUpdateSysMsgUnread,
                onofflinecustomsysmsgs: onOfflineCustomSysMsgs,
                oncustomsysmsg: onCustomSysMsg,
                onbroadcastmsg: onBroadcastMsg,
                onbroadcastmsgs: onBroadcastMsgs,
                onmsg: onMsg,

                onconnect: function () {
                    alert('SDK 连接成功')
                    // 连接成功后才能发消息

                },
                ondisconnect: function (obj) {
                    console.log('SDK 连接断开', obj)
                },
                onerror: function (error) {
                    console.log('SDK 连接失败', error)
                }
            })
        }
        function onFriends(friends) {
            console.log('收到好友列表', friends);
            data.friends = nim.mergeFriends(data.friends, friends);
            data.friends = nim.cutFriends(data.friends, friends.invalid);
            refreshFriendsUI();
        }
        function refreshFriendsUI() {
            // 刷新界面
        }
        function onRoamingMsgs(obj) {
            console.log('收到漫游消息', obj);
            //pushMsg(obj.msgs);
        }
        function onOfflineMsgs(obj) {
            console.log('收到离线消息', obj);
            //pushMsg(obj.msgs);
        }
        function onCustomMsg(msg) {
            // 处理自定义消息
        }
        function onMsg(msg) {
            console.log('收到消息', msg.scene, msg.type, msg);
            //pushMsg(msg);
            switch (msg.type) {
                case 'custom':
                    onCustomMsg(msg);
                    break;
                case 'notification':
                    // 处理群通知消息
                    onTeamNotificationMsg(msg);
                    break;
                // 其它case
                default:
                    break;
            }
        }


        function onOfflineSysMsgs(sysMsgs) {
            console.log('收到离线系统通知', sysMsgs);
            pushSysMsgs(sysMsgs);
        }
        function onSysMsg(sysMsg) {
            console.log('收到系统通知', sysMsg)
            pushSysMsgs(sysMsg);
        }
        function onUpdateSysMsg(sysMsg) {
            pushSysMsgs(sysMsg);
        }
        function pushSysMsgs(sysMsgs) {
            data.sysMsgs = nim.mergeSysMsgs(data.sysMsgs, sysMsgs);
            refreshSysMsgsUI();
        }
        function onSysMsgUnread(obj) {
            console.log('收到系统通知未读数', obj);
            data.sysMsgUnread = obj;
            refreshSysMsgsUI();
        }
        function onUpdateSysMsgUnread(obj) {
            console.log('系统通知未读数更新了', obj);
            data.sysMsgUnread = obj;
            refreshSysMsgsUI();
        }
        function refreshSysMsgsUI() {
            // 刷新界面
        }
        function onOfflineCustomSysMsgs(sysMsgs) {
            console.log('收到离线自定义系统通知', sysMsgs);
        }
        function onCustomSysMsg(sysMsg) {
            console.log('收到自定义系统通知', sysMsg);
        }
        function onBroadcastMsg(msg) {
            console.log('收到广播消息', msg);
        }
        function onBroadcastMsgs(msgs) {
            console.log('收到广播消息', msgs);
        }


    </script>
</head>
<body>

<div>


    <input type="button" value="初始化" onclick="initSDK()">
    <input type="button" value="发送消息" onclick="send()">
    <input type="button" value="直接添加为好友" onclick="addFriend()">
    <input type="button" value="申请添加为好友" onclick="applyFriend()">
    <input type="button" value="删除好友" onclick="deleteFriend()">
    <input type="button" value="通过好友申请" onclick="passFriendApply()">

</div>


</body>

<script>
    function passFriendApply() {
        console.info(data);
        nim.passFriendApply({
            idServer: data.sysMsgs[0].idServer,
            account: to_account,
            ps: 'ps',
            done: passFriendApplyDone
        });
    }
    function passFriendApplyDone(error, obj) {
        console.log(error);
        console.log(obj);
        console.log('通过好友申请' + (!error ? '成功' : '失败'));
        if (!error) {
            // onAddFriend(obj.friend);
        }
    }

    function deleteFriend() {
        nim.deleteFriend({
            account: to_account,
            done: deleteFriendDone
        });
    }
    function deleteFriendDone(error, obj) {
        console.log(error);
        console.log(obj);
        console.log('删除好友' + (!error ? '成功' : '失败'));
        if (!error) {
            // onDeleteFriend(obj.account);
        }
    }

    function applyFriend() {
        nim.applyFriend({
            account: to_account,
            ps: 'ps',
            done: function (error, obj) {
                console.log(error);
                console.log(obj);
                console.log('申请加为好友' + (!error ? '成功' : '失败'));
            }
        });

    }

    function addFriend() {
        nim.addFriend({
            account: '002',
            ps: 'ps',
            done: function (error, obj) {
                5201
                console.log(error);
                console.log(obj);
                console.log('直接加为好友' + (!error ? '成功' : '失败'));
                if (!error) {
                    onAddFriend(obj.friend);
                }
            }
        });
    }
    function send() {
        window.nim.sendText({
            scene: 'p2p',
            to: to_account,
            text: 'hi',
            done: function sendMsgDone(msg) {
                console.info(msg);
            }
        })
    }


</script>
</html>